package com.bj58.disconf.web.controllers;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bj58.disconf.web.common.constant.ErrorCode;
import com.bj58.disconf.web.common.utils.ParamUtil;
import com.bj58.disconf.web.controllers.auth.constant.LoginConstant;
import com.bj58.disconf.web.controllers.auth.login.RedisLogin;
import com.bj58.disconf.web.controllers.auth.validator.AuthValidator;
import com.bj58.disconf.web.service.sign.form.SigninForm;
import com.bj58.disconf.web.service.sign.service.SignMgr;
import com.bj58.disconf.web.service.user.bo.User;
import com.bj58.disconf.web.service.user.dto.Visitor;
import com.bj58.disconf.web.service.user.form.PasswordModifyForm;
import com.bj58.disconf.web.service.user.service.UserMgr;
import com.bj58.disconf.web.service.user.vo.VisitorVo;
import com.bj58.disconf.web.utils.GuiceDI;
import com.bj58.ub.common.commons.ThreadContext;
import com.bj58.wf.mvc.ActionResult;
import com.bj58.wf.mvc.BeatContext;
import com.bj58.wf.mvc.annotation.Path;
import com.google.gson.JsonObject;

@Path("/api/account")
public class UserController extends BaseController {

    protected static final Logger LOG = LoggerFactory.getLogger(UserController.class);

    /**
     * GET 获取
     *
     * @param
     *
     * @return
     */
    @Path("/session")
    public ActionResult get() {
        try {
            VisitorVo visitorVo = GuiceDI.getInstance(UserMgr.class).getCurVisitor();
            if (visitorVo != null) {
                return new JsonActionResult(buildSuccess("visitor", visitorVo));

            } else {
                // 没有登录啊
                return new JsonActionResult(buildGlobalError("syserror.inner", ErrorCode.GLOBAL_ERROR));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 登录
     *
     * @param signin
     * @param request
     *
     * @return
     */
    @Path("/signin")
    public ActionResult signin() {
        try {
            SigninForm signin = new SigninForm();
            signin.setName(ParamUtil.getString(beat.getRequest(), "name", ""));
            signin.setPassword(ParamUtil.getString(beat.getRequest(), "password", ""));
            signin.setRemember(ParamUtil.getInt(beat.getRequest(), "remember", 0));
            // 验证
            GuiceDI.getInstance(AuthValidator.class).validateLogin(signin);

            // 数据库登录
            User user = GuiceDI.getInstance(SignMgr.class).signin(signin.getName());

            // 过期时间
            int expireTime = LoginConstant.SESSION_EXPIRE_TIME;
            if (signin.getRemember().equals(1)) {
                expireTime = LoginConstant.SESSION_EXPIRE_TIME2;
            }

            // redis login
            GuiceDI.getInstance(RedisLogin.class).login(beat.getRequest(), user, expireTime);

            VisitorVo visitorVo = GuiceDI.getInstance(UserMgr.class).getCurVisitor();
            JsonObject json = buildSuccess("visitor", visitorVo);
            LOG.info(json.toString());
            return new JsonActionResult(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 登出
     *
     * @param request
     *
     * @return
     */
    @Path("/signout")
    public ActionResult signout(HttpServletRequest request) {

        try {
            GuiceDI.getInstance(RedisLogin.class).logout(request);

            return new JsonActionResult(buildSuccess("ok", "ok"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 修改密码
     *
     * @param
     *
     * @return
     */
    @Path("/password")
    public ActionResult password(@Valid PasswordModifyForm passwordModifyForm, HttpServletRequest request) {

        try {
            // 校验
            GuiceDI.getInstance(AuthValidator.class).validatePasswordModify(passwordModifyForm);

            // 修改
            Visitor visitor = ThreadContext.getSessionVisitor();
            GuiceDI.getInstance(UserMgr.class).modifyPassword(visitor.getLoginUserId(), passwordModifyForm.getNew_password());

            // re login
            GuiceDI.getInstance(RedisLogin.class).logout(request);

            return new JsonActionResult(buildSuccess("修改成功，请重新登录"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
